//用户端发来的修改请求
//用户信息修改

#include "staff.h"
int process_user_modify_request(int newfd,Msg *msg)
{
	printf("------------%s-----------%d.\n",__func__,__LINE__);
	char sql[DATALEN] = {0};
	char *errmsg;
	char **result;
	int nrow,ncolumn;
	sprintf(sql,"select *from usrinfo where id =%d",msg->info.id);
	if(sqlite3_get_table(db,sql,&result,&nrow,&ncolumn,&errmsg) != SQLITE_OK){
		printf("---****----%s.\n", errmsg);		
	}else{
		if(nrow == 0){
			strcpy(msg->recvmsg,"没有找到这个ID 请确认是否有这个工号\n");
			send(newfd,msg,sizeof(Msg),0);
		}else{
			switch(msg->flags){
			case ADDR:
				bzero(sql,sizeof(sql));
				sprintf(sql,"update usrinfo set address = '%s' where id = %d",msg->info.address,msg->info.id);
				if(sqlite3_get_table(db,sql,&result,&nrow,&ncolumn,&errmsg) != SQLITE_OK){
					printf("---****----%s.\n",errmsg);
					return -1;
				}else{
					strcpy(msg->recvmsg,"信息修改成功\n");
				}
				break;
			case PHONE:
				bzero(sql,sizeof(sql));
				sprintf(sql,"update usrinfo set telphone= '%s' where id = %d",msg->info.telphone,msg->info.id);
				if(sqlite3_get_table(db,sql,&result,&nrow,&ncolumn,&errmsg) != SQLITE_OK){
					printf("---****----%s.\n",errmsg);
					return -1;
				}else{
					strcpy(msg->recvmsg,"信息修改成功\n");
				}
				break;
			case PASSWORD:
				bzero(sql,sizeof(sql));
				sprintf(sql,"update usrinfo set passwd = '%s' where id = %d",msg->info.passwd,msg->info.id);
				if(sqlite3_get_table(db,sql,&result,&nrow,&ncolumn,&errmsg) != SQLITE_OK){
					printf("---****----%s.\n",errmsg);
					return -1;
				}else{
					strcpy(msg->recvmsg,"信息修改成功\n");
				}
				break;
			default:
				break;
			}
			send(newfd,msg,sizeof(Msg),0);
		}
	}
	return 0;
}

